package com.sky.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;

@RestController
@RequestMapping("/report")
@Slf4j
@Api(tags = "报告相关接口")
public class ReportController {

    /**
     * 测试接口，导出一个Execel文件
     */
    @GetMapping("/out_execel")
    @ApiOperation("导出报表")
    public void outExecel(HttpServletResponse response) throws IOException {
        // 读取项目根目录
        String property = System.getProperty("user.dir");
        String filePath = property + File.separator + "sky-server/src/main/resources/" + "poi-test.xlsx";
        FileInputStream inputStream = new FileInputStream(filePath);
        XSSFWorkbook execel = new XSSFWorkbook(inputStream);

        // 设置响应格式和下载文件名
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=report.xlsx");

        ServletOutputStream outputStream = response.getOutputStream();
        execel.write(outputStream);

        inputStream.close();
        outputStream.close();
        execel.close();

        log.info("导出成功");
    }
}
